package main.java.com.healthy.entity;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 饮食记录实体类
 * 对应功能模块：饮食记录与追踪模块
 */
public class DietRecord implements Serializable {
    private static final long serialVersionUID = 1L;

    private String recordId;
    private String userId;
    private String foodId;
    private String foodName;
    private double foodWeight; // 食物重量(g)
    private double calories; // 总热量
    private double carbohydrates; // 总碳水化合物
    private double protein; // 总蛋白质
    private double fat; // 总脂肪
    private double fiber; // 总膳食纤维
    private double sugar; // 总糖分
    private LocalDate recordDate;
    private String mealType; // 餐次：早餐、午餐、晚餐、上午加餐、下午加餐、夜宵
    private String mealTime; // 具体用餐时间
    private LocalDateTime createTime;
    private String notes; // 备注

    // 默认构造方法
    public DietRecord() {
        this.createTime = LocalDateTime.now();
        this.recordDate = LocalDate.now();
    }

    // 主要参数构造方法
    public DietRecord(String recordId, String userId, String foodId, String foodName,
                      double foodWeight, double calories, double carbohydrates,
                      double protein, double fat, LocalDate recordDate, String mealType) {
        this();
        this.recordId = recordId;
        this.userId = userId;
        this.foodId = foodId;
        this.foodName = foodName;
        this.foodWeight = foodWeight;
        this.calories = calories;
        this.carbohydrates = carbohydrates;
        this.protein = protein;
        this.fat = fat;
        this.recordDate = recordDate;
        this.mealType = mealType;
    }

    // 通过食物对象创建记录
    public DietRecord(String recordId, String userId, Food food, double foodWeight,
                      LocalDate recordDate, String mealType) {
        this();
        this.recordId = recordId;
        this.userId = userId;
        this.foodId = food.getFoodId();
        this.foodName = food.getFoodName();
        this.foodWeight = foodWeight;
        this.calories = food.calculateCalories(foodWeight);
        this.carbohydrates = food.calculateCarbohydrates(foodWeight);
        this.protein = food.calculateProtein(foodWeight);
        this.fat = food.calculateFat(foodWeight);
        this.fiber = food.calculateFiber(foodWeight);
        this.recordDate = recordDate;
        this.mealType = mealType;
    }

    // Getter和Setter方法
    public String getRecordId() {
        return recordId;
    }

    public void setRecordId(String recordId) {
        this.recordId = recordId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getFoodId() {
        return foodId;
    }

    public void setFoodId(String foodId) {
        this.foodId = foodId;
    }

    public String getFoodName() {
        return foodName;
    }

    public void setFoodName(String foodName) {
        this.foodName = foodName;
    }

    public double getFoodWeight() {
        return foodWeight;
    }

    public void setFoodWeight(double foodWeight) {
        this.foodWeight = foodWeight;
    }

    public double getCalories() {
        return calories;
    }

    public void setCalories(double calories) {
        this.calories = calories;
    }

    public double getCarbohydrates() {
        return carbohydrates;
    }

    public void setCarbohydrates(double carbohydrates) {
        this.carbohydrates = carbohydrates;
    }

    public double getProtein() {
        return protein;
    }

    public void setProtein(double protein) {
        this.protein = protein;
    }

    public double getFat() {
        return fat;
    }

    public void setFat(double fat) {
        this.fat = fat;
    }

    public double getFiber() {
        return fiber;
    }

    public void setFiber(double fiber) {
        this.fiber = fiber;
    }

    public double getSugar() {
        return sugar;
    }

    public void setSugar(double sugar) {
        this.sugar = sugar;
    }

    public LocalDate getRecordDate() {
        return recordDate;
    }

    public void setRecordDate(LocalDate recordDate) {
        this.recordDate = recordDate;
    }

    public String getMealType() {
        return mealType;
    }

    public void setMealType(String mealType) {
        this.mealType = mealType;
    }

    public String getMealTime() {
        return mealTime;
    }

    public void setMealTime(String mealTime) {
        this.mealTime = mealTime;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    // 更新营养成分（当食物或重量改变时调用）
    public void updateNutrition(Food food) {
        if (food != null) {
            this.calories = food.calculateCalories(foodWeight);
            this.carbohydrates = food.calculateCarbohydrates(foodWeight);
            this.protein = food.calculateProtein(foodWeight);
            this.fat = food.calculateFat(foodWeight);
            this.fiber = food.calculateFiber(foodWeight);
        }
    }

    // 获取餐次代码（用于排序）
    public int getMealTypeCode() {
        switch (mealType) {
            case "早餐": return 1;
            case "上午加餐": return 2;
            case "午餐": return 3;
            case "下午加餐": return 4;
            case "晚餐": return 5;
            case "夜宵": return 6;
            default: return 7;
        }
    }

    @Override
    public String toString() {
        return "DietRecord{" +
                "recordId='" + recordId + '\'' +
                ", userId='" + userId + '\'' +
                ", foodName='" + foodName + '\'' +
                ", foodWeight=" + foodWeight +
                ", calories=" + calories +
                ", carbohydrates=" + carbohydrates +
                ", protein=" + protein +
                ", fat=" + fat +
                ", recordDate=" + recordDate +
                ", mealType='" + mealType + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DietRecord that = (DietRecord) o;
        return recordId.equals(that.recordId);
    }

    @Override
    public int hashCode() {
        return recordId.hashCode();
    }
}